<template>
  <div class="car">
    <div class="aa1">
      <div class="title">
        <div class="left">已选商品</div>
        <div class="right" @click="clear">清空</div>
      </div>
      <div class="list">
        <div class="item" v-for="(item, index) in this.tasklist" :key="index">
          <div class="left">
            <div class="zuo">
              <img :src="item.picture" alt="" />
            </div>
            <div class="you">
              <div class="name">{{ item.name }}</div>
              <div class="jiage">
                <span class="xianjia">￥ {{ item.min_price }}</span>
              </div>
            </div>
          </div>
          <div class="right">
            <button class="jian" @click="sub(item)">-</button>
            <div>{{ item.count }}</div>
            <button class="jia" @click="add(item)">+</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 引入axios
import {
  shopcart_list,
  shopcart_sub,
  shopcart_add,
  shopcart_clear,
} from "../../api";

export default {
  mounted() {
    var userid = window.localStorage.getItem("userid");
    console.log(userid);
    // 发请求获取购物车列表
    shopcart_list({ userid: userid }).then((res) => {
      this.tasklist = res.data.list;
      console.log(this.tasklist);
    });
  },
  methods: {
    // 购物车商品减少
    sub(item) {
      var userid = window.localStorage.getItem("userid");
      console.log(item);
      shopcart_sub({ userid, goodid: item.id }).then((res) => {
        console.log(res.data);
      });
    },
    // 购物车商品数量增加
    add(item) {
      var userid = window.localStorage.getItem("userid");
      shopcart_add({ userid, goodid: item.id }).then((res) => {
        console.log(res.data);
      });
    },
    // 清空购物车
    clear() {
      var userid = window.localStorage.getItem("userid");
      shopcart_clear({ userid }).then((res)=>{
        console.log(res.data);
      })
    },
  },
  data() {
    return {
      tasklist: [],
    };
  },
};
</script>

<style scoped>
.car {
  background-color: white;
}
.car .title {
  display: flex;
  justify-content: space-between;
  padding: 10px;
}
.car .title .left {
  font-weight: 800;
}
.car .item {
  padding: 0 20px;
  display: flex;
  margin-bottom: 10px;
  align-items: center;
  justify-content: space-between;
}
.car .item .left {
  display: flex;
}
.car .item .left img {
  width: 80px;
  height: 80px;
}
.car .item .left .you {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin-left: 10px;
}
.car .item .left .you .name {
  font-size: 14px;
  font-weight: 800;
}
.car .item .left .you .xianjia {
  color: rgb(250, 62, 62);
}
.car .item .left .you .yuanjia {
  color: gray;
  font-size: 13px;
  text-decoration: line-through;
}
.car .item .right {
  display: flex;
}
.car .right button {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 0;
  text-align: center;
  line-height: 15px;
  margin-left: 10px;
  font-weight: 800;
}
.car .list .jian {
  color: blueviolet;
  border: 1px solid blueviolet;
  margin-right: 10px;
  background-color: white;
}
.car .jia {
  color: white;
  background-color: blueviolet;
}

.aa1 {
  background-color: white;
}
</style>